package com.ruoyi.project.system.submit.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.job.domain.JobCourseClass;
import com.ruoyi.project.system.job.mapper.JobCourseClassMapper;
import com.ruoyi.project.system.submit.mapper.JobSubmitMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.submit.domain.JobSubmit;
import com.ruoyi.project.system.submit.service.IJobSubmitService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 作业-提交记录Controller
 *
 * @author ruoyi
 * @date 2021-04-20
 */
@Controller
@RequestMapping("/system/submit")
public class JobSubmitController extends BaseController
{
    private String prefix = "system/submit";

    @Autowired
    private IJobSubmitService jobSubmitService;

    @Autowired
    private JobSubmitMapper jobSubmitMapper;

    @Autowired
    private JobCourseClassMapper jobCourseClassMapper;

    @GetMapping()
    public String submit(ModelMap mmap)
    {
        String roleName = ShiroUtils.getSysUser().getRoleName();
        mmap.put("roleName",roleName);
        return prefix + "/submit";
    }

    /**
     * 查询作业-提交记录列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JobSubmit jobSubmit)
    {
        startPage();
        // 获取角色名称
        List<JobSubmit> list = new ArrayList<>();
        if("学生".equals(ShiroUtils.getSysUser().getRoleName())){
            jobSubmit.setStudentId(ShiroUtils.getUserId());
            list = jobSubmitMapper.selectJobSubmitListGroupby(jobSubmit);

        }else if("教师".equals(ShiroUtils.getSysUser().getRoleName())){
            // 根据教师ID查询其关联的所有作业
            list = jobSubmitMapper.selectJobCourseClassListByTeacherId(ShiroUtils.getUserId());
        }
        return getDataTable(list);
    }

    /**
     * 导出作业-提交记录列表
     */
    @Log(title = "作业-提交记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JobSubmit jobSubmit)
    {
        List<JobSubmit> list = jobSubmitService.selectJobSubmitList(jobSubmit);
        ExcelUtil<JobSubmit> util = new ExcelUtil<JobSubmit>(JobSubmit.class);
        return util.exportExcel(list, "submit");
    }

    /**
     * 新增作业-提交记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存作业-提交记录
     */
    @Log(title = "作业-提交记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JobSubmit jobSubmit)
    {
        return toAjax(jobSubmitService.insertJobSubmit(jobSubmit));
    }

    /**
     * 修改作业-提交记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        JobSubmit jobSubmit = jobSubmitService.selectJobSubmitById(id);
        mmap.put("jobSubmit", jobSubmit);
        return prefix + "/edit";
    }

    /**
     * 修改作业-提交记录
     */
    @GetMapping("/submitJob/{id}")
    public String submitJob(@PathVariable("id") Long id, ModelMap mmap)
    {
        JobSubmit jobSubmit = jobSubmitService.selectJobSubmitById(id);
        List<JobCourseClass> list = jobCourseClassMapper.selectJobCourseClassById(jobSubmit.getJobId());

        // 查询所有题目
        JobCourseClass condition = new JobCourseClass();
        condition.setGroupId(list.get(0).getGroupId());
        List<JobCourseClass> jobCourseClassList = jobCourseClassMapper.selectJobCourseClassList(condition);
        if(jobCourseClassList.size() > 0){
            Integer index = 0;
            boolean flag = false;
            for (int i=0;i<jobCourseClassList.size();i++) {
                JobCourseClass courseClass = jobCourseClassList.get(i);
                if(StringUtils.isNotEmpty(courseClass.getOptionJson())){
                    JSONObject jsonObject =JSON.parseObject(courseClass.getOptionJson());
                    courseClass.setOption(jsonObject.getString("option"));
                    courseClass.setOptionanswer(jsonObject.getString("optionanswer"));
                }
                if(courseClass.getType().equals("选择题")){
                    if(flag){
                    }else {
                        flag = true;
                        index += 1;
                    }
                }else {
                    index +=1;
                    flag = false;
                }
                courseClass.setIndex(index);
                courseClass.setYouranswer(courseClass.getEnclosureUrl());
                if(StringUtils.isBlank(courseClass.getYouranswer())){
                    courseClass.setYouranswer("");
                }
            }
        }
        mmap.put("jobSubmit", jobSubmit);
        mmap.put("jobCourseClass", list.get(0));
        mmap.put("jobCourseClassList", jobCourseClassList);
        return prefix + "/edit";
    }

    @GetMapping("/piyue/{id}")
    public String piyue(@PathVariable("id") Long id, ModelMap mmap)
    {
        JobSubmit jobSubmit = jobSubmitService.selectJobSubmitById(id);
        List<JobCourseClass> jobCourseClassList = jobCourseClassMapper.selectJobCourseClassById(jobSubmit.getJobId());
        mmap.put("jobSubmit", jobSubmit);
        mmap.put("jobCourseClass", jobCourseClassList.get(0));

        // 查询所有题目
        JobCourseClass condition = new JobCourseClass();
        condition.setGroupId(jobSubmit.getGroupId());
        List<JobCourseClass> classList = jobCourseClassMapper.selectJobCourseClassList(condition);
        if(classList.size() > 0){
            Integer index = 0;
            boolean flag = false;
            for (JobCourseClass courseClass : classList) {
                if(StringUtils.isNotEmpty(courseClass.getOptionJson())){
                    JSONObject jsonObject =JSON.parseObject(courseClass.getOptionJson());
                    courseClass.setOption(jsonObject.getString("option"));
                    courseClass.setOptionanswer(jsonObject.getString("optionanswer"));
                }
                if(courseClass.getType().equals("选择题")){
                    if(flag){
                    }else {
                        flag = true;
                        index += 1;
                    }
                }else {
                    index +=1;
                    flag = false;
                }
                courseClass.setIndex(index);
            }
        }
        mmap.put("jobCourseClassList", classList);
        return prefix + "/piyue";
    }

    @GetMapping("/viewJob/{id}")
    public String viewJob(@PathVariable("id") Long id, ModelMap mmap)
    {
        JobSubmit jobSubmit = jobSubmitService.selectJobSubmitById(id);
        List<JobCourseClass> jobCourseClassList = jobCourseClassMapper.selectJobCourseClassById(jobSubmit.getJobId());
        mmap.put("jobSubmit", jobSubmit);
        mmap.put("jobCourseClass", jobCourseClassList.get(0));

        // 查询所有题目
        JobCourseClass condition = new JobCourseClass();
        condition.setGroupId(jobSubmit.getGroupId());
        List<JobCourseClass> classList = jobCourseClassMapper.selectJobCourseClassList(condition);
        if(classList.size() > 0){
            Integer index = 0;
            boolean flag = false;
            for (JobCourseClass courseClass : classList) {
                if(StringUtils.isNotEmpty(courseClass.getOptionJson())){
                    JSONObject jsonObject =JSON.parseObject(courseClass.getOptionJson());
                    courseClass.setOption(jsonObject.getString("option"));
                    courseClass.setOptionanswer(jsonObject.getString("optionanswer"));
                }
                if(courseClass.getType().equals("选择题")){
                    if(flag){
                    }else {
                        flag = true;
                        index += 1;
                    }
                }else {
                    index +=1;
                    flag = false;
                }
                courseClass.setIndex(index);
            }
        }
        mmap.put("jobCourseClassList", classList);
        return prefix + "/viewJob";
    }

    /**
     * 修改保存作业-提交记录
     */
    @Log(title = "作业-提交记录", businessType = BusinessType.UPDATE)
    @PostMapping("/piyue")
    @ResponseBody
    public AjaxResult piyue(@RequestBody JobSubmit jobSubmit)
    {
        return toAjax(jobSubmitService.piyue(jobSubmit));
    }

    /**
     * 修改保存作业-提交记录
     */
    @Log(title = "作业-提交记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody JobSubmit jobSubmit)
    {
        return toAjax(jobSubmitService.updateJobSubmit(jobSubmit));
    }

    /**
     * 删除作业-提交记录
     */
    @Log(title = "作业-提交记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jobSubmitService.deleteJobSubmitByIds(ids));
    }
}
